package UCAS;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

  public static void main(String[] args) throws IOException {

    System.out.println("请输入要制图的文件名（请使用绝对路径）:");

    // 我们需要从情绪记录图中读入数据
    // 首先输入文件名
    Scanner in = new Scanner(System.in);

    String fileName = in.nextLine();

    System.out.println(fileName);

    // 我们打开文件
    File file = new File(fileName);

    InputStreamReader reader = new InputStreamReader(new FileInputStream(file));

    BufferedReader br = new BufferedReader(reader);

    String line = "";

    // 跳过前8行数据
    for (int i = 0; i < 8; i++) {
      line = br.readLine();

      System.out.println(line);
    }

    line = br.readLine();

    //将数据存到三个数组中，第一个是横轴也就是xdata，第二个是Arousal，第三个是Valence
    ArrayList<Integer> xdataList = new ArrayList<>();
    ArrayList<Integer> arousalList = new ArrayList<>();
    ArrayList<Integer> valenceList = new ArrayList<>();

    while(line != null){

      //首先读入数据
      String[] elements = line.split(",");

      xdataList.add(Integer.valueOf(elements[0]));
      arousalList.add(Integer.valueOf(elements[1]));
      valenceList.add(Integer.valueOf(elements[2]));

      line = br.readLine();
    }

    //最后输出
    // System.out.println(xdataList);
    // System.out.println(arousalList);
    // System.out.println(valenceList);

    // 最后拼接处整个option的字符串
    String optionString =
        "option = {\n"
            + "    title: {\n"
            + "        text: 'Curve'\n"
            + "    },\n"
            + "    tooltip: {\n"
            + "        trigger: 'axis'\n"
            + "    },\n"
            + "    legend: {\n"
            + "        data:['Arousal', 'Valence']\n"
            + "    },\n"
            + "    grid: {\n"
            + "        left: '3%',\n"
            + "        right: '4%',\n"
            + "        bottom: '3%',\n"
            + "        containLabel: true\n"
            + "    },\n"
            + "    toolbox: {\n"
            + "        feature: {\n"
            + "            saveAsImage: {}\n"
            + "        }\n"
            + "    },\n"
            + "    xAxis: {\n"
            + "        type: 'category',\n"
            + "        boundaryGap: false,\n"
            + "        data: ";

    optionString  = optionString + xdataList + "\n";

    optionString = optionString + "},\n" +
        "    yAxis: {\n" +
        "        type: 'value'\n" +
        "    },\n" +
        "    series: [\n" +
        "        {\n" +
        "            name:'Arousal',\n" +
        "            type:'line',\n" +
        "            stack: '总量',\n" +
        "            data:";

    optionString = optionString + arousalList + "\n";

    optionString = optionString + "},\n" +
        "        {\n" +
        "            name:'Valence',\n" +
        "            type:'line',\n" +
        "            stack: '总量',\n" +
        "            data:";

    optionString = optionString + valenceList + "\n";

    optionString = optionString + "}\n" +
        "    ]\n" +
        "};\n";

    System.out.println();

    System.out.println();

    System.out.println("将下列代码复制到http://echarts.baidu.com/examples/editor.html?c=line-stack网页左侧（已经在剪切板中）");


    // 获取系统剪贴板
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

    // 封装文本内容
    Transferable trans = new StringSelection(optionString);

    // 把文本内容设置到系统剪贴板
    clipboard.setContents(trans, null);



    System.out.println(optionString);

    System.out.println();
    System.out.println("将上述代码复制到http://echarts.baidu.com/examples/editor.html?c=line-stack网页左侧（已经在剪切板中）");

  }
}
